<?php

namespace App\Observers;

use App\Models\Order;
use App\WeChat\WechatDefaultConfig;
use App\WechatMessages\ZPH\OrderCreatedMessage;

class OrderObserver
{
    use WechatDefaultConfig;

    public function creating(Order $order)
    {

    }

    public function created(Order $order)
    {
        //dd($order->items);
        $connect = $order->buyer->connect;
        if ($connect) {
            $item = $order->items->first;
            $message = new OrderCreatedMessage();
            $message->setTouser($connect->openid);
            $message->setProductName($item ? $item->title : '');
            $message->setAmount($order->total_fee);
            $message->setOrderTime(@date('Y-m-d H:i:s', $order->created_at));
            $message->setOrderNo($order->order_no);
            $this->officialAccount()->template_message->send($message->getMsgContent());
        }
    }

    public function updated(Order $order)
    {

    }
}
